#!/bin/sh

case $1 in
    prereqs)
        exit 0
        ;;
esac

# Extract kernel parameters
set -- $(cat /proc/cmdline)
for x in "$@"; do
    case "$x" in
        ssd-upgrader-part=*)
            ssd_upgrader_part="${x#ssd-upgrader-part=}"
            ;;
    esac
done

if [ ! -z "$ssd_upgrader_part" ]; then
    echo "ssd-upgrader-part found in /proc/cmdline" > /tmp/ssd-fw-upgrade.log
    mkdir -p /mnt/ssd_upgrader_part
    mount -t "${ssd_upgrader_part#*,}" "${ssd_upgrader_part%,*}" /mnt/ssd_upgrader_part
    if [ -x /mnt/ssd_upgrader_part/ssd-fw-upgrade ]; then
        cp /mnt/ssd_upgrader_part/ssd-fw-upgrade /tmp/
        cd /tmp/
        umount /mnt/ssd_upgrader_part
        rm -r /mnt/ssd_upgrader_part
        ./ssd-fw-upgrade >> /tmp/ssd-fw-upgrade.log 2>&1
    else
        echo "ssd-fw-upgrade not found" >> /tmp/ssd-fw-upgrade.log
        umount /mnt/ssd_upgrader_part
        rm -r /mnt/ssd_upgrader_part
    fi
    gzip /tmp/ssd-fw-upgrade.log
fi
